package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

   @Reference(check = false)
   private CheckGroupService checkGroupService;


   /**
    * 添加组
    * @param checkitemIds
    * @param checkGroup
    * @return
    */
   @RequestMapping("/add/{checkitemIds}")
   public Result add(@PathVariable Integer[] checkitemIds ,@RequestBody CheckGroup checkGroup) {

      try {
         checkGroupService.add(checkGroup, checkitemIds);
      } catch (Exception e) {
         e.printStackTrace();
         //添加失败
         return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
      }
      //添加成功
      return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
   }

   /**
    * 分页查询
    * @param queryPageBean
    * @return
    */
   @RequestMapping("/findPage")
   public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
      //System.out.println(queryPageBean.getPageSize()+"=="+queryPageBean.getCurrentPage()+"=="+queryPageBean.getQueryString());
      PageResult pageResult = checkGroupService.findPage(queryPageBean);

      return pageResult;
   }

   /**
    * 通过id查询
    * @param groupId
    * @return
    */
   @RequestMapping("/findById/{groupId}")
   public Result findById(@PathVariable Integer groupId) {
      CheckGroup checkGroup = null;
      try {
         checkGroup = checkGroupService.findById(groupId);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
      }
      return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
   }

   /**
    * 通过组id查询项id集合
    * @param groupId
    * @return
    */
   @RequestMapping("/findItemIdsByGroupId/{groupId}")
   public List<Integer> findItemIdsByGroupId(@PathVariable Integer groupId) {
      List<Integer> checkItemIds = checkGroupService.findItemIdsByGroupId(groupId);
      return checkItemIds;
   }

   /**
    * 修改检查组数据
    * @param checkitemIds
    * @param checkGroup
    * @return
    */
   @RequestMapping("/update/{checkitemIds}")
   public Result update(@PathVariable Integer[] checkitemIds ,@RequestBody CheckGroup checkGroup) {
      try {
         checkGroupService.update(checkitemIds,checkGroup);
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
      }
      return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
   }

   @RequestMapping("/findAll")
   public Result findAll() {

      try {
         List<CheckGroup> list = checkGroupService.findAll();
         //添加成功
         return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
      } catch (Exception e) {
         e.printStackTrace();
         //添加失败
         return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
      }

   }
}
